In the context of class, private means the attributes are only available for the members of the class not for the outside of the class.
- Suppose we have the following class which has private attributes (__alias):
We create an instance of class P, then trying to access its attributes whether it's public or private:
- For public attribute name, we can access through an instance variable, but not for the private attribute alias. Even we try this:
still we're not allowed to access it.
- But here is a magic want. One underscore('_') with the class name will do magic:
The the following call also works as expected:
- We're going to use the same code as in the previous section but we'll add two methods: foo() and __foo() methods:
How can we use the private method. If we try:
The right way of accessing private method is this:
- Of course, calling private method via public will work as expected: